/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict-local
 * @format
 * @oncall relay
 */

'use strict';

const {getRequest} = require('../../../query/GraphQLTag');
const {createReaderSelector} = require('../../../store/RelayModernSelector');
// We're creating a fragment in some file far away from this one to confirm the
// relative path generated by the compiler is correct.
//
// We're using the fragment below in `NoInlineTestQuery`, you can look in the artifact
// generated for that query to confirm the `require` is correct. Also, this test passing
// is confirmation that the relative path-ing works.
const SomeDeeplyNestedFragment = require('./some/deeply/nested/__generated__/SomeDeeplyNestedFragment.graphql');
const {graphql} = require('relay-runtime/query/GraphQLTag');
const {
  createOperationDescriptor,
} = require('relay-runtime/store/RelayModernOperationDescriptor');
const {createMockEnvironment} = require('relay-test-utils-internal');
const {
  disallowConsoleErrors,
  disallowWarnings,
} = require('relay-test-utils-internal');

disallowConsoleErrors();
disallowWarnings();

describe('No Inline w/ Common JS', () => {
  it('can read data', () => {
    const environment = createMockEnvironment();

    const NoInlineTestQuery = graphql`
      query NoInlineTestQuery {
        me {
          ...SomeDeeplyNestedFragment
        }
      }
    `;

    const request = getRequest(NoInlineTestQuery);
    const operation = createOperationDescriptor(request, {});
    environment.commitPayload(operation, {
      me: {
        id: '1',
        name: 'Alice',
      },
    });

    const fragmentSelector = createReaderSelector(
      SomeDeeplyNestedFragment,
      '1',
      {},
      operation.request,
    );

    // $FlowFixMe[unclear-type]
    const {data}: any = environment.lookup(fragmentSelector);

    expect(data.name).toEqual('Alice');
  });
});
